﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace BLL
{
    /// <summary>
    /// 地区设置, 业务逻辑处理
    /// </summary>
    public class RegionBase
    {
        /// <summary>
        /// 设备地区信息 添加
        /// </summary>
        /// <param name="categoryInfo">设备地区信息对象</param>
        public static string CreateNew(Model.RegionBase region)
        {
            region = ProcessUtility(region);
            if (region.Layer > 3)
            { 
                return "系统不允许添加超过三级的地区!"; 
            }
            else
            {
                return (new SQLServerDAL.RegionBase()).CreateNew(region); 
            }
        }

        /// <summary>
        /// 产品地区实体信息
        /// </summary>
        /// <param name="regionObj">设备地区实体对象</param>
        /// <returns></returns>
        private static Model.RegionBase ProcessUtility(Model.RegionBase regionObj)
        {
            DataTable dt = BLL.CommonBLL.Select("RegionBase", "*", " ORDER BY RegionName ASC");
            int parentId = regionObj.ParentId;

            string strNumber = "", strTemp = ""; int intNumber;
            if (dt.Rows.Count == 0)
            {
                regionObj.Layer = 1;
                regionObj.RegionNumber = "00001"; 
            }
            else
            {
                if (parentId == 0)
                {
                    regionObj.Layer = 1;
                    for (int n = 1; n < 10000; n++)
                    {
                        strTemp = "";
                        intNumber = dt.Select("Layer = 1").Length + n;
                        for (int i = 0; i < 5 - intNumber.ToString().Length; i++)
                            strTemp += "0";
                        regionObj.RegionNumber = strNumber + strTemp + intNumber.ToString();
                        if (dt.Select("RegionNumber = '" + regionObj.RegionNumber + "'").Length == 0)
                            break;
                    }
                }
                else
                {
                    DataRow dr = dt.Select(" RegionId = " + parentId)[0];
                    regionObj.Layer = Convert.ToInt32(dr["Layer"].ToString()) + 1;
                    strNumber = dr["RegionNumber"].ToString().Trim();

                    for (int n = 1; n < 10000; n++)
                    {
                        strTemp = "";
                        intNumber = dt.Select("Layer = " + regionObj.Layer + " and ParentID = '" + parentId + "'").Length + n;
                        for (int i = 0; i < 5 - intNumber.ToString().Length; i++)
                            strTemp += "0";
                        regionObj.RegionNumber = strNumber + strTemp + intNumber.ToString();
                        if (dt.Select(" RegionNumber = '" + regionObj.RegionNumber + "'").Length == 0)
                            break;
                    }
                }
            }
            return regionObj;
        }
 
        /// <summary>
        /// 修改地区
        /// </summary>
        /// <param name="subject">对象</param>
        /// <param name="intType">操作属性,0未修改父地区,1为修改父地区</param>
        public static string Modify(Model.RegionBase region, bool IsChangeParent)
        {
            region = ProcessUtility(region);
            if (region.Layer > 3)
            { 
                return "系统不允许添加超过三级的地区!";
            }
            else
            { 
                return (new SQLServerDAL.RegionBase()).Modify(region, IsChangeParent);
            }
        }
 
        /// <summary>
        /// 删除地区
        /// </summary> 
        public static string Delete(int regionId, bool isLogical)
        { 
            return (new SQLServerDAL.RegionBase()).Delete(regionId, isLogical);
        }

        /// <summary>
        /// 获取地区信息实体
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static Model.RegionBase GetObjById(int regionId)
        {
            return (new SQLServerDAL.RegionBase()).GetObjById(regionId); 
        }
    }
}
